/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.showcase.interpolation;

import static com.google.code.swingonfire.builder.JComponentBuilders.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import com.google.code.eventsonfire.Events;
import com.google.code.eventsonfire.awt.AWTEvents;
import com.google.code.eventsonfire.swing.SwingEventHandler;
import com.google.code.swingonfire.border.ShadowBorder;
import com.google.code.swingonfire.component.JBorderPanel;
import com.google.code.swingonfire.image.ImageCache;
import com.google.code.swingonfire.layout.GBC;
import com.google.code.swingonfire.showcase.TitlePanel;

public class InterpolationShowcase extends JPanel
{

    private static final long serialVersionUID = 7963835792263413164L;

    private final JComboBox interpolationComboBox;
    private final JButton playButton;
    private final JButton stopButton;
    private final InterpolationComponent interpolationComponent;

    public InterpolationShowcase()
    {
        super(new BorderLayout());

        interpolationComboBox =
            Events.bind(comboBoxBuilder().items(InterpolationSamples.values()).selected(InterpolationSamples.LINEAR)
                .listener("interpolation", AWTEvents.fireOnAction()).opaque(false).build(), this);
        playButton =
            Events.bind(buttonBuilder().icon(ImageCache.getIcon("play")).listener("play", AWTEvents.fireOnAction())
                .opaque(false).build(), this);
        stopButton =
            Events.bind(buttonBuilder().icon(ImageCache.getIcon("stop")).listener("stop", AWTEvents.fireOnAction())
                .opaque(false).build(), this);

        interpolationComponent = new InterpolationComponent();
        interpolationComponent.setBackground(Color.WHITE);
        interpolationComponent.setOpaque(true);

        JPanel panel =
            panelBuilder().layout(new GridBagLayout()).border(BorderFactory.createEmptyBorder(8, 12, 8, 12))
                .background(Color.WHITE).opaque(true).build();
        GBC gbc = new GBC(4, 1);

        panel.add(labelBuilder().text("Interpolation:").labelFor(interpolationComboBox).build(), gbc);
        panel.add(interpolationComboBox, gbc.next().hFill().weight(1));
        panel.add(playButton, gbc.next().insetRight(0));
        panel.add(stopButton, gbc.next().insetLeft(0));

        add(new JBorderPanel(new TitlePanel("Interpolations",
            "A demonstration of built-in interpolations for animations"), ShadowBorder.create()), BorderLayout.NORTH);
        add(new JBorderPanel(interpolationComponent, ShadowBorder.create()), BorderLayout.CENTER);
        add(new JBorderPanel(panel, ShadowBorder.create()), BorderLayout.SOUTH);

        onInterpolation();
    }

    protected void updateState()
    {
        if (interpolationComponent.isRunning())
        {
            playButton.setIcon(ImageCache.getIcon("pause"));
            stopButton.setEnabled(true);
        }
        else
        {
            playButton.setIcon(ImageCache.getIcon("play"));
            stopButton.setEnabled(false);
        }
    }

    @SwingEventHandler(event = ActionEvent.class, eachTag = "interpolation")
    public void onInterpolation()
    {
        interpolationComponent.setInterpolation(((InterpolationSamples) interpolationComboBox.getSelectedItem())
            .getInterpolation());

        updateState();
    }

    @SwingEventHandler(event = ActionEvent.class, eachTag = "play")
    public void onPlay()
    {
        if (interpolationComponent.isRunning())
        {
            interpolationComponent.pause();
        }
        else
        {
            interpolationComponent.reset(2);
            interpolationComponent.play();
        }

        updateState();
    }

    @SwingEventHandler(event = ActionEvent.class, eachTag = "stop")
    public void onStop()
    {
        interpolationComponent.stop();

        updateState();
    }

}
